package cmd

import (
	"context"

	"demo/internal/controller"
	"demo/internal/service"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
)

func MiddlewareLog(r *ghttp.Request) {

	ctx := context.TODO()

	g.Log().Print(ctx, r.Response.Status, r.URL.Path)

	r.Middleware.Next()
	errStr := ""
	if err := r.GetError(); err != nil {
		errStr = err.Error()
	}

	g.Log().Print(ctx, r.Response.Status, r.URL.Path, errStr)
}

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			// s.SetConfigWithMap(g.Map{
			// 	"AccessLogEnabled": false,
			// 	"ErrorLogEnabled":  false,
			// })
			s.Use(MiddlewareLog)
			s.Group("/", func(group *ghttp.RouterGroup) {

				group.Middleware(
					service.Middleware().Auth,
					ghttp.MiddlewareHandlerResponse,
				)

				group.Bind(
					controller.User,
				)

			})

			s.EnableAdmin()
			s.Run()
			return nil
		},
	}
)
